﻿using RealtorsPortal.Business_Logic_Layer;
using RealtorsPortal.Data_Access_Layer;
using RealtorsPortal.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RealtorsPortal.AdminCP
{
    public partial class MyAdvertisement : System.Web.UI.Page
    {
        AdvertisementsDAL advDAL = new AdvertisementsDAL();
        AdsImagesDAL adsImagDAL = new AdsImagesDAL();
        CategoriesDAL cateDAL = new CategoriesDAL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack){
                LoadAdvertiseToGridview();
                //Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "SelectAll();", true);
                btnDeleteSelected.Attributes.Add("OnClick", "javascript:return Confirmationbox()");
            }
            
        }

        private void LoadAdvertiseToGridview()
        {
            DataTable dt = new DataTable();
            AdvertisementsDAO advObj = new AdvertisementsDAO();
            advObj.UserId = Convert.ToInt16(Session["adminId"]);
            dt = advDAL.GetAdvertisements(advObj);
            
            grvManageMyAdver.DataSource = dt;
            grvManageMyAdver.DataBind();
        }

        protected void btnAddNewAdvertises_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/AdminCP/PostAdvertisement.aspx");
        }

        private void DeleteRowAtAdvertisement(int id){
            AdvertisementsDAO advObj = new AdvertisementsDAO();
            advObj.AdvId = id;
            advDAL.DeleteAdvertisements(advObj);
        }

        private int DeleteRowAtAdsImages(int id) {
            AdsImages adsImageObj = new AdsImages();
            adsImageObj.AdvId = id;
            return adsImagDAL.DeleteAdsImages(adsImageObj); 
        }

        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {

            foreach (GridViewRow gvrow in grvManageMyAdver.Rows)
            {
                CheckBox chkdelete = new CheckBox();
                //Finiding checkbox control in gridview for particular row
                chkdelete = (CheckBox)gvrow.FindControl("chkAdvertisements");
                
                //Condition to check checkbox selected or not
                if (chkdelete.Checked == true)
                {
                    //Getting UserId of particular row using datakey value

                    int id = Convert.ToInt32(grvManageMyAdver.DataKeys[gvrow.RowIndex].Value);
                    if (DeleteRowAtAdsImages(id)>0)
                    {
                        DeleteRowAtAdvertisement(id);
                    }
                }
            }
            LoadAdvertiseToGridview();
        }

        protected void grvManageMyAdver_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditComponent")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                string AdvId = grvManageMyAdver.DataKeys[index].Value.ToString(); 
                //Response.Redirect("~/AdminCP/EditAdvertisement.aspx?ID="+AdvId);
                Server.Transfer("~/AdminCP/EditAdvertisement.aspx?ID=" + AdvId);
            }
            else if (e.CommandName == "BrowseComponent")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                string AdvId = grvManageMyAdver.DataKeys[index].Value.ToString();
                //Response.Redirect("~/AdminCP/AdvertisementDetails.aspx/ID=" + AdvId);
                Server.Transfer("~/AdminCP/AdvertisementDetails.aspx?ID=" + AdvId);
            }
            else if (e.CommandName == "ReNewComponent")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                string AdvId = grvManageMyAdver.DataKeys[index].Value.ToString();
                AdvertisementsDAO advObj = new AdvertisementsDAO();
                advObj.AdvId = int.Parse(AdvId);
                DataTable dt = new DataTable();
                dt = advDAL.GetPackNameByAdvId(advObj);

                for (int i = 0; i < dt.Rows.Count; i++ )
                {
                    if (dt.Rows[0]["PackageName"].ToString() == "Free")
                    {
                        string message = "Sorry! You cannot renew your advertisement. Please pay fee!";
                        string url = "MyAdvertisement.aspx";
                        Utilities.ShowAndRedirect(message, url);
                    }
                    else {
                        Server.Transfer("~/AdminCP/RenewAdvertisement.aspx?ID=" + AdvId);
                    }
                }
                
            }
            //int passedId = Convert.ToInt32(Request.QueryString["IdPassed"])
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if(ddlFilter.SelectedItem.Text =="Category Name"){
                SearchByCategoryName();
            }else if(ddlFilter.SelectedItem.Text == "Title"){
                SearchByTitle();
            }
            
        }

        private void SearchByCategoryName() {
            DataTable dt = new DataTable();
            Category cateObj = new Category();
            cateObj.CategoryName = txtSearch.Text;
            dt = cateDAL.GetCateByCategoryName(cateObj);
            if (dt.Rows.Count > 0)
            {
                int categoryId = int.Parse(dt.Rows[0]["CategoryId"].ToString());
                AdvertisementsDAO advObj = new AdvertisementsDAO();
                advObj.CategoryId = categoryId;
                dt = advDAL.GetAdByCateId(advObj);
                if (dt.Rows.Count > 0)
                {
                    grvManageMyAdver.DataSource = dt;
                    grvManageMyAdver.DataBind();
                }
                else
                {
                    Utilities.Show("Cannot find any results");
                    return;
                }
            }
            else
            {
                Utilities.Show("Cannot find any results");
                return;
            }
            
        }

        private void SearchByTitle() {
            DataTable dt = new DataTable();
            AdvertisementsDAO advObj = new AdvertisementsDAO();
            advObj.Title = txtSearch.Text;
            dt = advDAL.GetAdByTile(advObj);
            if (dt.Rows.Count > 0)
            {
                grvManageMyAdver.DataSource = dt;
                grvManageMyAdver.DataBind();
            }
            else
            {
                Utilities.Show("Cannot find any results");
                return;
            }
        }

        protected void grvManageMyAdver_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvManageMyAdver.PageIndex = e.NewPageIndex;
            this.LoadAdvertiseToGridview();
        }

    }
}